package fun.xming.memlib;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.content.Intent;
import android.widget.Toast;
import java.util.HashMap;

import fun.xming.memlib.tools.UnitConversion;

public class AddMemActivity extends Activity {
	String name;
    LinearLayout model;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avtivity_addmem);
    }
	public void alert(String str) {
		Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
	}
	@Override
	protected void onResume() {
		super.onResume();
		model = findViewById(R.id.addmain_col_1);
		Intent intent = getIntent();
	    name = intent.getStringExtra("name");
	}

    public void addOnClick(View view) {
        LinearLayout btnlay = findViewById(R.id.add_btn_lay);
        LinearLayout main = findViewById(R.id.addmem_main);
		
		main.addView(createNewColomn(),main.getChildCount()-1);
    }

    public void completeOnClick(View view)  {
		LinearLayout main = findViewById(R.id.addmem_main);
		HashMap map = new HashMap<String,HashMap>();
		for(int i = 0;i<main.getChildCount()-1;i++) {
			HashMap cmap = new HashMap<String,String>();
			LinearLayout card = (LinearLayout)( main.getChildAt(i));
			String v = String.valueOf(((EditText)(card.getChildAt(1))).getText());
			String k = String.valueOf(((EditText)(card.getChildAt(0))).getText());
			//MainActivity.alert(String.valueOf(t.getText()));
			if(v.isEmpty() || k.isEmpty() || map.containsKey(k)) {
				MainActivity.alert("Some key is repeated or null");
				return;
			}
			cmap.put("context",v);
			cmap.put("last-time","0");
			cmap.put("last-mis-count","0");
			cmap.put("S","1200");
			map.put(k,cmap);
			//alert(map.toString());
			//try{
			//	Thread.sleep(1000);
			//}catch(Exception e){}
			
		}
		//alert(map.toString());
		//try{
		//Thread.sleep(1000);
		//}catch(Exception e){}
		MainActivity.getMemManager().addMem(name,map);
		this.finish();
    }

    private LinearLayout createNewColomn() {
		LinearLayout l = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.card_add_word_column,null,false);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(model.getLayoutParams());
		lp.setMargins(UnitConversion.dipToPx(this,20),UnitConversion.dipToPx(this,5),UnitConversion.dipToPx(this,20),UnitConversion.dipToPx(this,5));
		//lp.height = 50;
		//lp.width = 280;
		l.setLayoutParams(lp);
		return l;
    }
    
}
